﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Text;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebProxy
{
    public partial class NotificationManager : System.Web.UI.Page
    {
        public const string emptyFieldMessage = "Kurka wodna czyżbyś nie wypełnił wszystkich wymaganych pól?";
        public const string successAddRecord = "Gratulacje dodałeś rekord do bazy danych";
        protected void Button1_Click(object sender, EventArgs e)
        {
            string tbLifeSpan = Request.Form["tbLifeSpan"];
            string tbTimePicked = Request.Form["tbTimePicked"];
            string tbLink = Request.Form["tbLink"];
            string tbContent = Request.Form["tbContent"];
            string tbTitle = Request.Form["tbTitle"];

            if (tbTitle != string.Empty && tbContent != string.Empty &&
                tbTimePicked != string.Empty && tbLifeSpan != string.Empty)
            {

                DateTime avaFrom = Convert.ToDateTime(tbTimePicked);
                DateTime avaTo = avaFrom.AddMinutes(Convert.ToDouble(tbLifeSpan));

                SQLiteDatabase db = new SQLiteDatabase();
                db.InsertNotification(new NetcampNotification(
                    tbTitle, tbContent, tbLink, avaFrom, avaTo
                    ));
                showAlert(successAddRecord);
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            }
            else
            {
                showAlert(emptyFieldMessage);
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            string rfTimeSpan = Request.Form["rfTimeSpan"];
            string rfTimePicked = Request.Form["rfTimePicked"];
            string rfRefreshValue = Request.Form["rfRefreshValue"];

            if (rfTimeSpan != string.Empty && rfTimePicked != string.Empty && rfRefreshValue != string.Empty)
            {
                DateTime avaFrom = Convert.ToDateTime(rfTimePicked);
                DateTime avaTo = avaFrom.AddMinutes(Convert.ToDouble(rfTimeSpan));
                SQLiteDatabase db = new SQLiteDatabase();
                db.InsertRefresh(avaFrom.Ticks, avaTo.Ticks, Convert.ToInt32(rfRefreshValue) );
                showAlert(successAddRecord);
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            }
            else
            {
                showAlert(emptyFieldMessage);
            }

        }
        private void showAlert(string msg)
        {
            string script = "<script type=\"text/javascript\">alert('" + msg + "');</script>";
            RegisterClientScriptBlock("alert", script);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SQLiteDatabase data = new SQLiteDatabase();
                data.DeleteOldItems();
            }

        }

        
    }
}